<?php
include_once('TemplateBase.php');

class ApplicationTemplate extends TemplateBase
{
	public function printTemplate(ContentBase $ContentBase = null, $ContentID = null) {
		ob_start();	
		$templateFilePath = $_SERVER['DOCUMENT_ROOT'] .'/'. $this->templateFilePath;

		//load the file contents 
		$fileContents = file_get_contents($templateFilePath);		
		//calculate the template root
		$templateRoot = substr($this->templateFilePath,0,strrpos($this->templateFilePath,"\\"));
		if(trim($templateRoot) == "") {
			$templateRoot = substr($this->templateFilePath,0,strrpos($this->templateFilePath,"/"));
			$templateRoot = $templateRoot . "/";
		}
		else {
			$templateRoot = $templateRoot . "\\";
		}
		$fileContents = str_replace("{TEMPLATE_ROOT}",$templateRoot,$fileContents);
		
		//load the main content
		if($ContentBase != null && $page = $ContentBase->GetContent($ContentID))
		{			
			$content_string = $page['content'];
			
			//handle the advanced content
			$found_flag = ereg("{ADV_INCLUDE}([^{]*){/ADV_INCLUDE}",$content_string,$array);
			while($found_flag > 0 && $page['advanced'] == 1) {
				if(!@include($array[1])) {
					$out =  '<span style="color:red;"><strong>Living Web Error</strong><br>Could not include advanced content <em>' . $array[1] . '</em></span>'; 
				}
				else {
					$out = ob_get_contents();
				}
				ob_clean();
				ob_start();	
				
				$content_string = ereg_replace("{ADV_INCLUDE}([^{]*){/ADV_INCLUDE}",$out,$content_string);
				$found_flag = ereg("{ADV_INCLUDE}([^{]*){/ADV_INCLUDE}",$content_string,$array);
			}
			
			if(!isset($page['display_menu']) || $page['display_menu'] <> 1)
			{
				print '<style> .menu { display: none; } </style>';
			}

			//page div/title 
			if(isset($page['title'])) {	$title = $page['title']; }
			else { $title = null; }
			if(isset($page['ministry'])) { $ministry = $page['ministry']; }
			else { $ministry = null; }
			$fileContents = str_replace("{PAGE_TITLE}",$title,$fileContents);
			$fileContents = str_replace("{PAGE_DIV}",$ministry,$fileContents);
		}
		$fileContents = str_replace("{CONTENT}",$content_string,$fileContents);

		//let the derrived classes replace what they need to			
		$fileContents = $this->replaceTemplateVariables($fileContents);
		
		//print the contents after all of the template variables have been replaced
		print $fileContents;

		ob_end_flush();
	}
	
	public function replaceTemplateVariables($fileContents) {		
		$date_time = getdate();
		$date_string = $date_time['mon'] . '/' . $date_time['mday'] . '/' . $date_time['year'];
		$time_string = $date_time['hours'] . ':' . $date_time['minutes'] . ':' . $date_time['seconds'];

		$count = 1;
		$this->menu_num = count($this->menu);
		while($count <= $this->menu_num)
		{	
			$menu_contents = "";
			$menu_title = "";
			if($count <= count($this->menu))
			{
				$menu_contents = $this->menu[$count];
				if(isset($this->menu_title))
				{
					$menu_title = $this->menu_title[$count];
				}
			}
			
			$regex = "{MENU_" . $count . "}";
			$fileContents = str_replace($regex,$menu_contents,$fileContents);
			$regex = "{MENU_" . $count . "_TITLE}";
			$fileContents = str_replace($regex,$menu_title,$fileContents);

			$count++;		
		}
		$fileContents = str_replace("{TITLE}",$this->title,$fileContents);
		$fileContents = str_replace("{DATE}",$date_string,$fileContents);
		$fileContents = str_replace("{TIME}",$time_string,$fileContents);
					
		if(strpos($this->sectionSkipFlags,",") > 0)
		{
			$array_of_ssfs = split(",",$this->sectionSkipFlags);
			
			foreach($array_of_ssfs as $flag_num)
			{
				$regex = "{SECTION_" . $flag_num . "}.*{SECTION_" . $flag_num . "/}";
				$fileContents = ereg_replace($regex,"",$fileContents);
			}
		}
		else
		{
			$flag_num = $this->sectionSkipFlags;
			$regex = "{SECTION_" . $flag_num . "}.*{SECTION_" . $flag_num . "/}";
			$fileContents = ereg_replace($regex,"",$fileContents);
		}

		$fileContents = ereg_replace("{SECTION_[^}]*}","",$fileContents);
		$fileContents = ereg_replace("{SECTION_[^/]*/}","",$fileContents);
				
		return $fileContents;
	}
}
?>
